/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.ui.edge;

import java.awt.Container;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import net.worlds.Main;


public class EStickySwingFrame extends EStickyFrame{
    int componentX;
    int componentY;
    int componentWidth;
    int componentHeight;
    JComponent component;
    Container container;
    public EStickySwingFrame(EComponent parent, int width, int height, int left, int right, int top, int bottom, JComponent c)
    {
        super(parent, width, height, left, right, top, bottom);
        setComponent(c);
    }
    public EStickySwingFrame(EComponent parent, int width, int height, int left, int right, int top, int bottom)
    {
        super(parent, width, height, left, right, top, bottom);
    }
    public EStickySwingFrame(EComponent parent, int width, int height, int left, int right, int top, int bottom, JPanel c)
    {
        super(parent, width, height, left, right, top, bottom);
        c.setSize(getWidth() - 10, getHeight() - 10);
        container = c;
        visible = false;
    }
    protected void setComponent(JComponent c)
    {
        c.setSize(getWidth() - 10, getHeight() - 10);
        component = c;
        visible = false;
    }
    public void setVisible(boolean v)
    {
        visible = v;
        if(Main.noView)
            return;
        if(v)
        {
            if(component == null)
            {
                container.setSize(getWidth() - 10, getHeight() - 10);
                Main.getApplet().addContainer(container);
                container.setLocation(getX() + 5, getY() + 5);
            }
            else
            {
                component.setSize(getWidth() - 10, getHeight() - 10);
                container = Main.getApplet().addComponent(component);
                container.setLocation(getX() + 5, getY() + 5);
            }
        }
        else
        {
            if(component == null)
            {
                Main.getApplet().removeContainer(container);
            }
            else
            {
                Main.getApplet().removeComponent(container);
                container = null;
            }
        }
    }
    @Override
    public int getX()
    {
        final int sx = super.getX();
        if(container != null)
        {
            if(sx + 5 != componentX)
            {
                componentX = sx + 5;
                SwingUtilities.invokeLater(new Runnable()
                {

                    public void run() {
                        container.setLocation(sx + 5, container.getY());
                    }

                });
            }
        }
        return sx;
    }
    @Override
    public int getWidth()
    {
        int w = super.getWidth();
        if(w != componentWidth && container != null)
        {
            container.setSize(w - 10, container.getHeight());
            componentWidth = w;
        }
        return w;
    }
    @Override
    public int getHeight()
    {
        int h = super.getHeight();
        if(h != componentHeight && container != null)
        {
            container.setSize(container.getWidth(), h - 10);
            componentWidth = h;
        }
        return h;
    }
    @Override
    public void setHeight(int height)
    {
        super.setHeight(height);
        if(container != null)
            container.setSize(getWidth() - 10, height - 10);
    }
    @Override
    public void setY(int y)
    {
        super.setY(y);
        if(container != null)
            container.setLocation(getX() + 5, y + 5);
    }
    @Override
    public int getY()
    {
        final int sy = super.getY();
        if(container != null && sy + 5 != componentY)
        {
            componentY = sy + 5;
            SwingUtilities.invokeLater(new Runnable()
            {

                public void run() {
                    container.setLocation(container.getX(), sy + 5);
                }

            });
        }
        return sy;
    }
}
